<template>
    <div class="login-container">
        <div class="login-box">
            <div class="login-header">
                <h2>登录</h2>
                <p>欢迎来到PMall积分商城</p>
            </div>

            <el-form ref="loginFormRef" :model="loginForm" :rules="loginRules" class="login-form"
                @submit.prevent="handleLogin">
                <el-form-item prop="username">
                    <el-input v-model="loginForm.username" placeholder="请输入用户名" size="large" prefix-icon="User"
                        clearable />
                </el-form-item>

                <el-form-item prop="password">
                    <el-input v-model="loginForm.password" type="password" placeholder="请输入密码" size="large"
                        prefix-icon="Lock" show-password clearable @keyup.enter="handleLogin" />
                </el-form-item>

                <el-form-item>
                    <el-button type="primary" size="large" :loading="userStore.loading" @click="handleLogin"
                        class="login-button">
                        {{ userStore.loading ? '登录中...' : '登录' }}
                    </el-button>
                </el-form-item>
            </el-form>

            <div class="login-footer">
                <p>
                    还没有账号？
                    <router-link to="/register" class="register-link">
                        立即注册
                    </router-link>
                </p>
            </div>
        </div>
    </div>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { ElMessage, type FormInstance, type FormRules } from 'element-plus'
import { useUserStore } from '@/stores/user'
import type { LoginRequest } from '@/types'

const router = useRouter()
const route = useRoute()
const userStore = useUserStore()

// 表单引用
const loginFormRef = ref<FormInstance>()

// 登录表单数据
const loginForm = reactive<LoginRequest>({
    username: '',
    password: ''
})

// 表单验证规则
const loginRules: FormRules = {
    username: [
        { required: true, message: '请输入用户名', trigger: 'blur' },
        { min: 3, max: 20, message: '用户名长度在 3 到 20 个字符', trigger: 'blur' }
    ],
    password: [
        { required: true, message: '请输入密码', trigger: 'blur' },
    ]
}

// 处理登录
const handleLogin = async () => {
    if (!loginFormRef.value) return

    try {
        // 验证表单
        await loginFormRef.value.validate()

        // 调用登录接口
        const success = await userStore.login(loginForm)

        if (success) {
            // 登录成功，根据角色跳转
            if (userStore.isAdmin) {
                router.push('/admin/dashboard')
            } else {
                const redirect = route.query.redirect as string
                router.push(redirect || '/')
            }
        }
    } catch (error) {
        console.error('登录失败:', error)
    }
}

// 组件挂载时检查是否已登录
onMounted(() => {
    if (userStore.isAuthenticated) {
        if (userStore.isAdmin) {
            router.push('/admin/dashboard')
        } else {
            router.push('/')
        }
    }
})
</script>

<style scoped>
.login-container {
    min-height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    padding: 20px;
}

.login-box {
    width: 100%;
    max-width: 400px;
    background: white;
    border-radius: 12px;
    box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
    padding: 40px;
}

.login-header {
    text-align: center;
    margin-bottom: 30px;
}

.login-header h2 {
    color: #333;
    margin-bottom: 10px;
    font-size: 28px;
    font-weight: 600;
}

.login-header p {
    color: #666;
    font-size: 14px;
}

.login-form {
    margin-bottom: 20px;
}

.login-button {
    width: 100%;
    height: 45px;
    font-size: 16px;
    font-weight: 500;
}

.login-footer {
    text-align: center;
    color: #666;
    font-size: 14px;
}

.register-link {
    color: #409eff;
    text-decoration: none;
    font-weight: 500;
}

.register-link:hover {
    text-decoration: underline;
}

/* 响应式设计 */
@media (max-width: 480px) {
    .login-box {
        padding: 30px 20px;
    }

    .login-header h2 {
        font-size: 24px;
    }
}
</style>
